<template>
  <div class="login-container">
    <div class="box">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>黑马面经运营后台</span>
        </div>

        <el-form
          status-icon
          label-position="top"
          label-width="100px"
          class="demo-ruleForm"
          :model="data"
          :rules="rules"
          @keydown.enter.native="submitForm(data)"
        >
          <el-form-item label="用户名" prop="username">
            <el-input type="text" v-model="data.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model.number="data.password"></el-input>
          </el-form-item>
          <el-form-item class="footer">
            <el-button type="primary" @click="submitForm(data)">登录</el-button>
            <el-button @click="resetForm()">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { LoginApi } from '@/api/user'
import { setToken } from '@/utils/storage'

export default {
  name: 'LoginIndex',
  data () {
    return {
      data: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async submitForm (data) {
      const res = await LoginApi(data)
      setToken(res.data.token)
      this.$message({
        message: '登录成功',
        type: 'success'
      })
      this.$router.push('/')
    },
    resetForm () {
      this.data = {}
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  min-height: 100vh;
  background-image: url('@/assets/login-bg.svg');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  // background: url(@/assets/login-bg.svg) no-repeat 50% / cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 420px;
    ::v-deep .el-card__header {
      padding: 0;
    }
    ::v-deep .el-form-item__label {
      display: block;
      width: 100%;
    }
    .clearfix {
      background-color: #727cf5;
      color: #fff;
      height: 80px;
      font-size: 18px;
      line-height: 80px;
      text-align: center;
    }
    ::v-deep .el-form.demo-ruleForm.el-form--label-top {
      padding: 0 20px;

      .el-form-item__label {
        padding: 0;
      }
    }
    .footer {
      margin-top: 20px;
      display: flex;
      width: 100%;
      justify-content: center;
    }
  }
}
</style>
